package com.module.admin.app.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.module.admin.BaseController;
import com.module.admin.app.pojo.AppInfo;
import com.module.admin.app.pojo.AppPrj;
import com.module.admin.app.pojo.AppUser;
import com.module.admin.app.service.AppInfoService;
import com.module.admin.app.service.AppPrjService;
import com.module.admin.app.service.AppUserService;
import com.module.admin.sys.pojo.SysUser;
import com.system.comm.model.KvEntity;
import com.system.comm.model.Page;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;

/**
 * app_info的Controller
 * @author admin
 * @date 2020-12-27 20:12:45
 * @version V1.0.0
 */
@Controller
public class AppInfoController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(AppInfoController.class);

	@Autowired
	private AppInfoService appInfoService;
	@Autowired
	private AppPrjService appPrjService;
	@Autowired
	private AppUserService appUserService;
	

	@RequestMapping(value = "/appInfo/f-view/manager")
	public String manager(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return "admin/app/appInfo-manager";
	}

	@RequestMapping(value = "/appInfo/f-view/add")
	public String add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return "admin/app/appInfo-add";
	}
	
	@RequestMapping(value = "/appInfo/f-view/edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, String aiId) {
		if(aiId != null) {
			modelMap.put("appInfo", appInfoService.get(aiId));
		}
		return "admin/app/appInfo-edit";
	}

	@RequestMapping(value = "/appInfo/f-json/pageQuery")
	@ResponseBody
	public void pageQuery(HttpServletRequest request, HttpServletResponse response, AppInfo appInfo,
			Integer isQueryAll) {
		ResponseFrame frame = new ResponseFrame();
		try {
			if (isQueryAll == null || isQueryAll != 1) {
				SysUser user = getSessionUser(request);
				appInfo.setCreateUserId(user.getUserId());
			}
			Page<AppInfo> page = appInfoService.pageQuery(appInfo);
			frame.setBody(page);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("分页获取信息异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appInfo/f-json/save")
	@ResponseBody
	public void save(HttpServletRequest request, HttpServletResponse response, AppInfo appInfo) {
		ResponseFrame frame = null;
		try {
			SysUser user = getSessionUser(request);
			appInfo.setCreateUserId(user.getUserId());
			frame = appInfoService.saveOrUpdate(appInfo);
		} catch (Exception e) {
			LOGGER.error("异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appInfo/f-json/delete")
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response, String aiId) {
		ResponseFrame frame = null;
		try {
			frame = appInfoService.delete(aiId);
		} catch (Exception e) {
			LOGGER.error("删除异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appInfo/f-json/pageQueryPrj")
	@ResponseBody
	public void pageQueryPrj(HttpServletRequest request, HttpServletResponse response,
			AppPrj appPrj) {
		ResponseFrame frame = new ResponseFrame();
		try {
			Page<AppPrj> page = appPrjService.pageQuery(appPrj);
			frame.setBody(page);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("分页获取信息异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appInfo/f-json/savePrj")
	@ResponseBody
	public void savePrj(HttpServletRequest request, HttpServletResponse response, AppPrj appPrj) {
		ResponseFrame frame = null;
		try {
			SysUser user = getSessionUser(request);
			appPrj.setCreateUserId(user.getUserId());
			frame = appPrjService.save(appPrj);
		} catch (Exception e) {
			LOGGER.error("异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}
	
	@RequestMapping(value = "/appInfo/f-json/deletePrj")
	@ResponseBody
	public void deletePrj(HttpServletRequest request, HttpServletResponse response,
			String aiId, Integer prjId) {
		ResponseFrame frame = null;
		try {
			frame = appPrjService.delete(aiId, prjId);
		} catch (Exception e) {
			LOGGER.error("删除异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appInfo/f-json/pageQueryUser")
	@ResponseBody
	public void pageQueryUser(HttpServletRequest request, HttpServletResponse response,
			AppUser appUser) {
		ResponseFrame frame = new ResponseFrame();
		try {
			Page<AppUser> page = appUserService.pageQuery(appUser);
			frame.setBody(page);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("分页获取信息异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appInfo/f-json/saveUser")
	@ResponseBody
	public void saveUser(HttpServletRequest request, HttpServletResponse response, AppUser appUser) {
		ResponseFrame frame = null;
		try {
			SysUser user = getSessionUser(request);
			appUser.setCreateUserId(user.getUserId());
			frame = appUserService.save(appUser);
		} catch (Exception e) {
			LOGGER.error("异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}
	
	@RequestMapping(value = "/appInfo/f-json/deleteUser")
	@ResponseBody
	public void deleteUser(HttpServletRequest request, HttpServletResponse response,
			String aiId, Integer userId) {
		ResponseFrame frame = null;
		try {
			frame = appUserService.delete(aiId, userId);
		} catch (Exception e) {
			LOGGER.error("删除异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appInfo/f-json/findSearchPrj")
	@ResponseBody
	public void findSearchPrj(HttpServletRequest request, HttpServletResponse response,
			String searchString, String aiId) {
		ResponseFrame frame = new ResponseFrame();
		try {
			List<KvEntity> list = appPrjService.findSearch(searchString, aiId);
			frame.setBody(list);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("获取信息异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}
}